package com.czz.oss.controller;

import com.czz.common.vo.R;
import com.czz.oss.service.FileUploadService;
import com.czz.oss.utils.ConstantPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ClassName: FileUploadUserController
 * @Author Czz
 * @Date 2020/2/16
 * @Time 10:30
 **/
@RestController
@RequestMapping(value = "/user/file")
public class FileUploadUserController {

    @Autowired
    FileUploadService fileUploadService;

    /**
     * @Description 上传文件至阿里云oss
     * @Param [file, host, courseId, chapterId]
     * @Return com.czz.common.vo.R
     * @Author Czz
     * @Date 2020/1/30
     * @Time 18:25
     **/
    @PostMapping(value = "/upload")
    public R fileUpload(
            @RequestParam(value = "file") MultipartFile file,
            @RequestParam(value = "host",required = false) String host,
            @RequestParam(value = "courseId", required = false) String courseId
    ){
        if (!StringUtils.isEmpty(host)){
            ConstantPropertiesUtil.FILE_HOST = host;
        }
        Map data = fileUploadService.uploadFile(file,courseId);
        return R.ok().message("文件上传成功").data("item",data);
    }
}
